package com.matrix.quartz.core;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.Map;

import com.matrix.quartz.contance.QuartzContance;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.matrix.core.tools.LogUtil;
import com.matrix.quartz.bean.QuartzExecuteHistory;
import com.matrix.quartz.dao.QuartzExecuteHistoryDao;

/**
 * quartz执行类的封装类
 * 
 * @author zhangheng
 * @email 512061637@qq.com
 * @date 2019年1月15日
 */
public abstract class ExtJob implements Job {

	private QuartzExecuteHistory quartzExecuteHistory = null;

	@Autowired
	private QuartzExecuteHistoryDao quartzExecuteHistoryDao;
	
	/**
	 * 执行成功
	 */
	private final static String EXECUTE_STATUS_1="1";
	
	/**
	 * 执行失败
	 */
	private final static String EXECUTE_STATUS_2="2";

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		Map<String, Object> paramMap = context.getMergedJobDataMap();
		Integer jdId = Integer.valueOf((String) paramMap.get(QuartzContance.JOB_JDID_KEY));
		// 插入
		// 任务开始前
		before(jdId);
		boolean flag = false;
		String exceptionDetail = null;
		try {
			flag = this.executeExt(context);
		} catch (Exception e) {
			// 若执行方法出错 继续往下走 保存异常信息
			exceptionDetail = getExceptionAllinformation(e);
			LogUtil.error("任务执行异常,任务id#{}", e, jdId);
		}

		// 写入执行状态
		after(flag, exceptionDetail);
	}

	public abstract boolean executeExt(JobExecutionContext context);

	/**
	 * 定时任务开始方法 新建任务历史
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 2019年1月14日
	 * @param jdId
	 */
	private void before(Integer jdId) {
		// 任务执行前 需要新增历史记录表数据
		quartzExecuteHistory = new QuartzExecuteHistory();
		quartzExecuteHistory.setCreateBy(QuartzContance.USER_NAME);
		Date startDate = new Date();
		quartzExecuteHistory.setEhStartTime(startDate);
		quartzExecuteHistory.setUpdateBy(QuartzContance.USER_NAME);
		quartzExecuteHistory.setJdId(jdId);

	}

	/**
	 * 定时任务结束方法 插入定时任务历史表
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 2019年1月14日
	 * @param flag
	 * @param exceptionDetail
	 */
	public void after(boolean flag, String exceptionDetail) {
		// 任务执行之后
		// 更新状态
		quartzExecuteHistory.setEhEndTime(new Date());
		if (flag) {
			// 成功
			quartzExecuteHistory.setEhExecueStatus(EXECUTE_STATUS_1);
		} else {
			quartzExecuteHistory.setEhExecueStatus(EXECUTE_STATUS_2);
			// 失败 插入错误详情
			quartzExecuteHistory.setEhExceptionDetail(exceptionDetail);
		}
		if (null != this.quartzExecuteHistoryDao) {
			LogUtil.info("插入定时任务执行记录");
			this.quartzExecuteHistoryDao.insert(quartzExecuteHistory);
		}
		LogUtil.info("定时任务执行完毕#{}", quartzExecuteHistory.getJdId());
	}

	public void setQuartzExecuteHistoryDao(QuartzExecuteHistoryDao quartzExecuteHistoryDao) {
		this.quartzExecuteHistoryDao = quartzExecuteHistoryDao;
	}

	/**
	 * 获取异常信息
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 2019年1月17日
	 * @param ex
	 * @return
	 */
	private String getExceptionAllinformation(Exception ex) {
		StringWriter stringWriter = new StringWriter();
		PrintWriter writer = new PrintWriter(stringWriter);
		ex.printStackTrace(writer);
		StringBuffer buffer = stringWriter.getBuffer();
		return buffer.toString();
	}

}
